/*
 * @(#)CollectionManager.java
 * 
 * Copyright(C) 2004 Yujin Kim
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.netexplode.jtunes.model.media;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.cdavies.itunes.Track;

import com.netexplode.jtunes.Constants;

/**
 * <code>CollectionManager</code> represents .....
 * 
 * @author ykim
 * @version $Revision: 1.2 $
 * @since 0.1
 */
public class CollectionManager {

    private int collectionType = Constants.COLLECTION_TYPE_LOCAL;

    private List localCollections = new ArrayList();

    private int localPosition;

    private List sharedCollections = new ArrayList();

    private int sharedPosition;

    private static CollectionManager instance = null;

    public static CollectionManager getInstance() {
        if (instance == null) {
            synchronized (CollectionManager.class) {
                instance = new CollectionManager();
            }
        }

        return instance;
    }

    public MediaCollection newInstance(String name, String source)
            throws IOException {
        return new LocalCollection(name, source);
    }

    public MediaCollection newInstance() throws IOException {
        return new SharedCollection();
    }

    /**
     * @return Returns the collectionType.
     */
    public int getCollectionType() {
        return this.collectionType;
    }

    /**
     * @param collectionType
     *            The collectionType to set.
     */
    public void setCollectionType(int collectionType) {
        this.collectionType = collectionType;
    }

    /**
     * @return Returns the localCollections.
     */
    public List getLocalCollections() {
        return localCollections;
    }

    /**
     * @param localCollections
     *            The localCollections to set.
     */
    public void setLocalCollections(List collections) {
        this.localCollections = collections;
    }

    /**
     * @return Returns the localPosition.
     */
    public int getLocalPosition() {
        return this.localPosition;
    }

    /**
     * @param localPosition
     *            The localPosition to set.
     */
    public void setLocalPosition(int localPosition) {
        this.localPosition = localPosition;
    }

    /**
     * @return Returns the sharedCollections.
     */
    public synchronized List getSharedCollections() {
        return this.sharedCollections;
    }

    /**
     * @param sharedCollections
     *            The sharedCollections to set.
     */
    public synchronized void setSharedCollections(List sharedCollections) {
        this.sharedCollections = sharedCollections;
    }

    /**
     * @return Returns the sharedPosition.
     */
    public int getSharedPosition() {
        return this.sharedPosition;
    }

    /**
     * @param sharedPosition
     *            The sharedPosition to set.
     */
    public void setSharedPosition(int sharedPosition) {
        this.sharedPosition = sharedPosition;
    }

    private int getSongPosition() {
        return CollectionManager
                .getInstance()
                .getCurrrentCollection()
                .getPosition();
    }

    // private int getCollectionPosition() {
    // switch (collectionType) {
    // case Constants.COLLECTION_TYPE_LOCAL:
    // return this.getLocalPosition();
    // case Constants.COLLECTION_TYPE_SHARED:
    // return this.getSharedPosition();
    // default:
    // return 0;
    //        }
    //    }

    public void movePrevious() {
        int songIndex = this.getSongPosition();
        if (songIndex == 0) {
            songIndex = this.getCurrrentCollection().size() - 1;
        }
        else {
            songIndex--;
        }
        this.getCurrrentCollection().setPosition(songIndex);
    }

    public void moveNext() {
        int songIndex = this.getSongPosition();
        if (songIndex == (this.getCurrrentCollection().size() - 1)) {
            songIndex = 0;
        }
        else {
            songIndex++;
        }
        this.getCurrrentCollection().setPosition(songIndex);
    }

    public MediaCollection getCurrrentCollection() {

        switch (collectionType) {
        case Constants.COLLECTION_TYPE_LOCAL:
            return (MediaCollection) this.getLocalCollections().get(
                    this.getLocalPosition());
        case Constants.COLLECTION_TYPE_SHARED:
            return (MediaCollection) this.getSharedCollections().get(
                    this.getLocalPosition());
        default:
            return null;
        }
    }

    public MediaFile getCurrentMediaFile() {

        switch (collectionType) {
        case Constants.COLLECTION_TYPE_LOCAL:
            return this.getCurrrentCollection().getMediaFile(
                    this.getSongPosition());
        case Constants.COLLECTION_TYPE_SHARED:
            final SharedCollection sharedCol = (SharedCollection) this
                    .getSharedCollections()
                    .get(this.getSharedPosition());
            Track track = sharedCol.getTracks()[this.getSongPosition()];
            return new ItunesFile(track);
        default:
            return null;
        }
    }

    public Track getCurrentItunesTrack() {
        return (Track) this
                .getSharedCollections()
                .get(this.getSharedPosition());
    }
}